package com.light.boot_demo.service.impl;


import com.light.boot_demo.event.service.SimpleServiceEvent;
import com.light.boot_demo.mapper.RecyclerMapper;
import com.light.boot_demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
//注解的事务回滚是默认RunTimeException才会回滚
//SpringMVC说异常统一在controller处理，
@Slf4j
public class UserServiceImpl implements UserService {

    @Value("${test.prop}")
    String testProp;

    @Autowired
    RecyclerMapper mapper;

    @Autowired
    ApplicationEventPublisher eventPublisher;


    public String aaa() {
        // 先执行aop

        mapper.update();

        log.info("Method Start-----------------");

        // transcation 回调，在aop后面,但是异常还是没有处理，只是throw ，统一在controller层处理
        eventPublisher.publishEvent(
                new SimpleServiceEvent(this, "执行到了service方法了")
        );

        // exception here
        int i = 1 / 0;
//        throw new ArithmeticException("aaa");

        // 发生exception后面是不执行的
        log.info("Method Ends-----------------");
        return "hello world";
    }
}
